<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>thenable.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="local_closure_goog_promise_thenable.js.html">thenable.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2013 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>goog.provide(&#39;goog.Thenable&#39;);
<a name="line16"></a>
<a name="line17"></a>
<a name="line18"></a>
<a name="line19"></a>/**
<a name="line20"></a> * Provides a more strict interface for Thenables in terms of
<a name="line21"></a> * http://promisesaplus.com for interop with {@see goog.Promise}.
<a name="line22"></a> *
<a name="line23"></a> * @interface
<a name="line24"></a> * @extends {IThenable.&lt;TYPE&gt;}
<a name="line25"></a> * @template TYPE
<a name="line26"></a> */
<a name="line27"></a>goog.Thenable = function() {};
<a name="line28"></a>
<a name="line29"></a>
<a name="line30"></a>/**
<a name="line31"></a> * Adds callbacks that will operate on the result of the Thenable, returning a
<a name="line32"></a> * new child Promise.
<a name="line33"></a> *
<a name="line34"></a> * If the Thenable is fulfilled, the {@code onFulfilled} callback will be
<a name="line35"></a> * invoked with the fulfillment value as argument, and the child Promise will
<a name="line36"></a> * be fulfilled with the return value of the callback. If the callback throws
<a name="line37"></a> * an exception, the child Promise will be rejected with the thrown value
<a name="line38"></a> * instead.
<a name="line39"></a> *
<a name="line40"></a> * If the Thenable is rejected, the {@code onRejected} callback will be invoked
<a name="line41"></a> * with the rejection reason as argument, and the child Promise will be rejected
<a name="line42"></a> * with the return value of the callback or thrown value.
<a name="line43"></a> *
<a name="line44"></a> * @param {?(function(this:THIS, TYPE):
<a name="line45"></a> *             (RESULT|IThenable.&lt;RESULT&gt;|Thenable))=} opt_onFulfilled A
<a name="line46"></a> *     function that will be invoked with the fulfillment value if the Promise
<a name="line47"></a> *     is fullfilled.
<a name="line48"></a> * @param {?(function(*): *)=} opt_onRejected A function that will be invoked
<a name="line49"></a> *     with the rejection reason if the Promise is rejected.
<a name="line50"></a> * @param {THIS=} opt_context An optional context object that will be the
<a name="line51"></a> *     execution context for the callbacks. By default, functions are executed
<a name="line52"></a> *     with the default this.
<a name="line53"></a> * @return {!goog.Promise.&lt;RESULT&gt;} A new Promise that will receive the result
<a name="line54"></a> *     of the fulfillment or rejection callback.
<a name="line55"></a> * @template RESULT,THIS
<a name="line56"></a> */
<a name="line57"></a>goog.Thenable.prototype.then = function(opt_onFulfilled, opt_onRejected,
<a name="line58"></a>    opt_context) {};
<a name="line59"></a>
<a name="line60"></a>
<a name="line61"></a>/**
<a name="line62"></a> * An expando property to indicate that an object implements
<a name="line63"></a> * {@code goog.Thenable}.
<a name="line64"></a> *
<a name="line65"></a> * {@see addImplementation}.
<a name="line66"></a> *
<a name="line67"></a> * @const
<a name="line68"></a> */
<a name="line69"></a>goog.Thenable.IMPLEMENTED_BY_PROP = &#39;$goog_Thenable&#39;;
<a name="line70"></a>
<a name="line71"></a>
<a name="line72"></a>/**
<a name="line73"></a> * Marks a given class (constructor) as an implementation of Thenable, so
<a name="line74"></a> * that we can query that fact at runtime. The class must have already
<a name="line75"></a> * implemented the interface.
<a name="line76"></a> * Exports a &#39;then&#39; method on the constructor prototype, so that the objects
<a name="line77"></a> * also implement the extern {@see goog.Thenable} interface for interop with
<a name="line78"></a> * other Promise implementations.
<a name="line79"></a> * @param {function(new:goog.Thenable,...[?])} ctor The class constructor. The
<a name="line80"></a> *     corresponding class must have already implemented the interface.
<a name="line81"></a> */
<a name="line82"></a>goog.Thenable.addImplementation = function(ctor) {
<a name="line83"></a>  goog.exportProperty(ctor.prototype, &#39;then&#39;, ctor.prototype.then);
<a name="line84"></a>  if (COMPILED) {
<a name="line85"></a>    ctor.prototype[goog.Thenable.IMPLEMENTED_BY_PROP] = true;
<a name="line86"></a>  } else {
<a name="line87"></a>    // Avoids dictionary access in uncompiled mode.
<a name="line88"></a>    ctor.prototype.$goog_Thenable = true;
<a name="line89"></a>  }
<a name="line90"></a>};
<a name="line91"></a>
<a name="line92"></a>
<a name="line93"></a>/**
<a name="line94"></a> * @param {*} object
<a name="line95"></a> * @return {boolean} Whether a given instance implements {@code goog.Thenable}.
<a name="line96"></a> *     The class/superclass of the instance must call {@code addImplementation}.
<a name="line97"></a> */
<a name="line98"></a>goog.Thenable.isImplementedBy = function(object) {
<a name="line99"></a>  if (!object) {
<a name="line100"></a>    return false;
<a name="line101"></a>  }
<a name="line102"></a>  try {
<a name="line103"></a>    if (COMPILED) {
<a name="line104"></a>      return !!object[goog.Thenable.IMPLEMENTED_BY_PROP];
<a name="line105"></a>    }
<a name="line106"></a>    return !!object.$goog_Thenable;
<a name="line107"></a>  } catch (e) {
<a name="line108"></a>    // Property access seems to be forbidden.
<a name="line109"></a>    return false;
<a name="line110"></a>  }
<a name="line111"></a>};
</pre>


</body>
</html>
